package net.wofly.sale.stock.validator;

import net.wofly.sale.stock.domain.InOutStockDetail;
import net.wofly.sale.stock.domain.InStock;
import net.wofly.sale.stock.domain.OutStock;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * Created by LiuJianbo on 2017/10/15.
 */
public class StockValidator {
    
    /**
     * 换行符
     */
    private static String lineSeparator = System.getProperty("line.separator");
    
    public static String validateAddInStockParams(InStock inStock) {
        StringBuffer stringBuffer = new StringBuffer();
        if (inStock == null) {
            stringBuffer.append("入库对象不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(inStock.getInStockType())) {
                stringBuffer.append("入库类型不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(inStock.getInStocker())) {
                stringBuffer.append("入库人不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(inStock.getProofID())) {
                stringBuffer.append("入库单据号不能为空").append(lineSeparator);
            }
            if (CollectionUtils.isEmpty(inStock.getInOutStockDetails())) {
                stringBuffer.append("入库详情不能为空").append(lineSeparator);
            } else {
                for (InOutStockDetail detail : inStock.getInOutStockDetails()) {
                    if (detail.getCommodityTerminalID() == null) {
                        stringBuffer.append("入库详情的终端商品ID不能为空").append(lineSeparator);
                    }
                    if (detail.getNum() == null || detail.getNum() <= 0) {
                        stringBuffer.append("入库详情的商品数量不能为空，并且数量不能少于1").append(lineSeparator);
                    }
                }
            }
        }
        
        return stringBuffer.toString();
    }
    
    public static String validateAddOutStockParams(OutStock outStock) {
        StringBuffer stringBuffer = new StringBuffer();
        if (outStock == null) {
            stringBuffer.append("出库对象不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(outStock.getOutStockType())) {
                stringBuffer.append("出库类型不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(outStock.getOutStocker())) {
                stringBuffer.append("出库人不能为空").append(lineSeparator);
            }
            if (StringUtils.isEmpty(outStock.getProofID())) {
                stringBuffer.append("出库单据号不能为空").append(lineSeparator);
            }
            if (CollectionUtils.isEmpty(outStock.getInOutStockDetails())) {
                stringBuffer.append("出库详情不能为空").append(lineSeparator);
            } else {
                for (InOutStockDetail detail : outStock.getInOutStockDetails()) {
                    if (detail.getCommodityTerminalID() == null) {
                        stringBuffer.append("出库详情的终端商品ID不能为空").append(lineSeparator);
                    }
                    if (detail.getNum() == null || detail.getNum() <= 0) {
                        stringBuffer.append("出库详情的商品数量不能为空，并且数量不能少于1").append(lineSeparator);
                    }
                }
            }
        }
        
        return stringBuffer.toString();
    }
    
    public static String validateWarningParams(String warehouseID, String commodityTypeID, String
    		commodityTerminalID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(warehouseID)) {
            stringBuffer.append("仓库ID(warehouseID)不能为空").append(lineSeparator);
        }
        if (StringUtils.isEmpty(commodityTypeID)) {
            stringBuffer.append("商品分类ID(commodityTypeID)不能为空").append(lineSeparator);
        }
        if (StringUtils.isEmpty(commodityTerminalID)) {
            stringBuffer.append("终端商品ID（commodityTerminalID）不能为空").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateSetStockWarningParams(String warehouseID, String commodityTypeID, String
            commodityChannelID, Integer num) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(validateWarningParams(warehouseID, commodityTypeID, commodityChannelID));
        if (num == null || num <= 0) {
            stringBuffer.append("出库详情的商品数量不能为空，并且数量不能少于1").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateEmployeeID(String employeeID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(employeeID)) {
            stringBuffer.append("employeeID不能为空").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateEmployeeID(String companyOrgID ,String shopOrgID,String employeeID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(companyOrgID) && StringUtils.isEmpty(shopOrgID) && StringUtils.isEmpty(employeeID)) {
            stringBuffer.append("companyOrgID 与 shopOrgID 及 employeeID 必须有一个不为空！").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
}
